package org.frame.security.project.system.controller;

import java.util.List;
import java.util.Set;

import org.frame.common.core.AjaxResult;
import org.frame.common.core.entity.TokenUser;
import org.frame.common.core.entity.User;
import org.frame.security.common.utils.ServletUtils;
import org.frame.security.framework.security.service.SysPermissionService;
import org.frame.security.framework.security.service.TokenService;
import org.frame.security.project.system.domain.SysMenu;
import org.frame.security.project.system.domain.SysUser;
import org.frame.security.project.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController {

	@Autowired
	private ISysMenuService menuService;

	@Autowired
	private SysPermissionService permissionService;

	@Autowired
	private TokenService tokenService;

	/**
	 * 获取用户信息
	 * 
	 * @return 用户信息
	 */
	@GetMapping("getInfo")
	public AjaxResult getInfo() {
		TokenUser tokenUser = tokenService.getTokenUser(ServletUtils.getRequest());
		User u = tokenUser.getUser();
		
		SysUser user = new SysUser();
		user.setUserId(u.getUserId());
		// 角色集合
		Set<String> roles = permissionService.getRolePermission(user);
		// 权限集合
		Set<String> permissions = permissionService.getMenuPermission(user);
		AjaxResult ajax = AjaxResult.success();
		ajax.put("user", u);
		ajax.put("roles", roles);
		ajax.put("permissions", permissions);
		return ajax;
	}

	/**
	 * 获取路由信息
	 * 
	 * @return 路由信息
	 */
	@GetMapping("getRouters")
	public AjaxResult getRouters() {
		TokenUser tokenUser = tokenService.getTokenUser(ServletUtils.getRequest());
		// 用户信息
		User u = tokenUser.getUser();
		List<SysMenu> menus = menuService.selectMenuTreeByUserId(u.getUserId());
		return AjaxResult.success(menuService.buildMenus(menus));
	}
}
